// 搜索框的构造函数
function Search(obj){
    this.search = obj.search;
    this.list = obj.list;
    this.enterSearch();
    this.listChange();
}
Search.prototype = {
    constructor : Search,
    // 查找输入框的内容
    searchSomething : function (){
        // 获取输入框的内容
        let text = this.search.value;
        //调用判断函数判断text是否为空或者空格
        if( this.judgeNull(text) ) return;
        // 将输入框的内容链接到百度查询
        let html = `http://www.baidu.com/s?wd=${text}`;
        // 在新窗口打开链接
        window.open(html);
    },
    // 按enter查询
    enterSearch : function (){
        let that = this;
        // 当enter建被按下时调用
        this.search.onkeydown = function (e){
            e = e || window.event;
            if(e.keyCode === 13){
                // 调用查询函数
                that.searchSomething();
                // 调用移除联想框list的函数
                that.removeList();
            }
        }
    },
    listChange : function(){
        // 存this对象
        that = this;
        // 搜索框改变时调用函数
        this.search.oninput = function (){
            // 显示联想框
            that.list.style.display = "block";
            // 获取搜索框的内容
            let text = this.value;
            //判断是否为空或者空格
            if( that.judgeNull(text) ) {
                // 返回true调用隐藏联想框函数
                that.removeList();
                return;
            }
            // 创建一个script节点
            let oS = document.createElement("script");
            // src链接到百度的联想词的地址
            oS.src = `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${text}&cb=that.thinkSearch`;
            // 添加script节点
            document.body.appendChild(oS);
            // 当此节点加载完毕后移除此节点
            oS.onload = function(){
                document.body.removeChild(oS);
            }
        }
    },
    // 联想框内容函数，回调函数
    thinkSearch : function (obj){
        // 接收百度的联想词地址返回的obj对象中的联想词数组
        let arr = obj.s;
        let html = "";
        let that = this;
        // 将arr数组拼接到联想框的a标签内
        for(let i = 0, len = arr.length; i < len; i++){
            html += `<li><a href="http://www.baidu.com/s?wd=${arr[i]}" target="_black" onclick="that.removeList()">${arr[i]}</a></li>`;
            this.list.innerHTML = html;
        }
    },
    // 判断是否是空或者全部是空格的函数
    judgeNull : function(text){
        let reg = /^\s*$/g;//全部为空格的正则
        // 判断是否是空或者全部是空格
        if (!text || reg.test(text)){
            return true;
        }else {return false;}
    },
    // 清空、隐藏联想框
    removeList : function(){
        this.list.innerHTML = "";
        this.list.style.display = "none";
    },
}

let obj = new Search({
    search : document.getElementById("search"),
    list : document.getElementById("list"),
});



// 雪花
let timer1 = null;

function bulidSnow(){
    let snow = document.createElement("div");
    snow.className = "snow",
    oBody = document.body,
    bw = oBody.clientWidth,
    bh = oBody.clientHeight;
    snow.style.left = `${Math.random()*bw}px`;
    snow.style.top = `${Math.random()*611}px`;
    for(let i = 0; i < 4; i++){
        let line = document.createElement("div");
        line.className =  "line"+i;
        snow.appendChild(line);
    }
    addSnow(snow);
}
timer1 = setInterval( bulidSnow, 1000);

function addSnow(snow){
    document.body.appendChild(snow);
    let opacity = 0;
    let timer = setInterval(function (){
        opacity += 0.1;
        if (opacity > 1) {
            clearInterval(timer);
            runSnow(snow);
        }
        snow.style.opacity = opacity;
    }, 1000/60)

}
function runSnow(snow){
    let sTop = snow.offsetTop,
        sLeft = snow.offsetLeft;
    function flySnow(){
        sTop--;
        sLeft--;
        snow.style.top = sTop+"px";
        snow.style.left = sLeft+"px";
        if(sTop === 0 && sLeft === 0){
            cancelAnimationFrame(flySnow);
            snow.parentNode.removeChild(snow);
        }
        requestAnimationFrame(flySnow);
    }
    requestAnimationFrame(flySnow);
}


let x = 90;
rotateTime();
function rotateTime(){
    let oCube = document.getElementById("cube"),
        nowTime = new Date().toTimeString(),
        reg = /\d{2}:\d{2}:\d{2}/g,
        oP = oCube.getElementsByTagName("p");
    oCube.style.transform = `rotateX(${x}deg)`;

    for (let i = 0; i < oCube.children.length; i++) {
        let color = "#";
        for (let j = 0; j < 6; j++) {
        color += Math.floor(Math.random()*16).toString(16);
        }
        oCube.children[i].style.background = color;
    }
    for (let i = 0, len = oP.length; i < len; i++) {
        oP[i].innerHTML = nowTime.match(reg);
    }
    x += 90;
}
let timer2;
timer2 = setInterval(rotateTime,1000);


window.onblur = function (){
    timer2 = window.clearInterval(timer2);
    timer1 = window.clearInterval(timer1);
    if (timer1 && timer2) alert("解除定时器出错！");
}
window.onfocus = function (){
    timer2 = window.setInterval(rotateTime,1000);
    timer1 = window.setInterval( bulidSnow, 1000);
}
